package com.ssy.wireless.api.advice;

import com.ssy.wireless.util.ReqVo;
import com.ssy.wireless.util.RespVo;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
public class RestControllerRequestAdvice {

    @Value("${application.api.security.msgKey}")
    private String msgKey;

    @Pointcut("execution(public * com.ssy.wireless.api.controller.*Controller*.**(..)))")
    public void init() {
    }

    @Around(value = "init()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        boolean flag = false;
        for (Object arg : args) {
            if (arg instanceof ReqVo && ((ReqVo) arg).getMsgKey().equals(msgKey)) {
                flag = true;
            }
        }
        return flag ? pjp.proceed() : RespVo.error(403, "您没有权限访问当前资源");
    }
}
